#!/usr/bin/env python
#
# This file displays the sorted list of (event, dateTime) in html
#

from datetime import datetime
import os

def createHeader(eventList):
	header = "\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\"http://www.w3.org/TR/html4/loose.dtd\">"
	header += "\n<html lang=\"en\">"
	header += "\n<head><title>Chrono Search</title></head>" 
	
	return header

def createBody(eventList):
	body = "\n<body>"
	year = 0

	for event in eventList:
		# new year heading?
		if year == 0:
			year = event.date.year
			body += "\n<h1>%d</h1>" % year
			body += "\n<ul>"
			
		elif year != event.date.year:
			year = event.date.year
			# end previous list
			body += "</ul>"
			body += "\n<h1>%d</h1>" % year
			body += "\n<ul>"
			
			
		body += "<li>"
		body += "\n<b>%s</b>" %( event.date.strftime("%Y-%m-%d"))
		body += "\n\t <a href=\"%s\">%s</a>" % (event.url, event.sentence)
		body += "</li>"
	
	body += "</ul>"	
	body += "\n</body>"
	return body

	
def createClosing(eventList):
	closing = "</html>"
	return closing


def display(eventList, fileName):
	eventList.reverse()
	# open output file"
	directory = os.path.dirname(fileName)
	if not os.path.exists(directory):
		os.makedirs(directory)
	
	file = open(fileName, 'w')
	header = createHeader(eventList)
	file.write(header)
	file.write("\n\n\n")

	body = createBody(eventList)
	file.write(body)
	file.write("\n\n\n")

	closing = createClosing(eventList)
	file.write(closing)

	file.close()
	eventList.reverse()


	
############ MAIN ###################	

#date1 = datetime(2011, 5, 18)
#date2 = datetime(2011, 1, 30)
#date3 = datetime(2010, 12, 15)
#date4 = datetime(2010, 6, 7)
#date5 = datetime(2009, 9, 18)
#date6 = datetime(2005, 4, 3)

#eventList = [	(date1, "Test sentence with <b>bold html</b>"),
#				(date2, "Test five.................."),
#				(date3, "Test four two one asdfasdfadf"),
#				(date4, "Test three"),
#				(date5, "Test date two"),
#				(date6, "Test date one"),
#display(eventList, "./displayOut.html")
			

	
